package repository

import (
	"energy-amiot-platform/model/dbspo"

	"gorm.io/gorm"
)

var (
	MQTTSENDER mqttsenderRepo
)

type mqttsenderRepo struct {
}

func (p *mqttsenderRepo) Exist(db *gorm.DB, key uint64) (*gorm.DB, bool) {
	var count int64
	tx := db.Model(&dbspo.MqttSender{}).Where("product_id=?", key).Count(&count)
	return tx, count > 0
}

func (p *mqttsenderRepo) ExistWithUserName(db *gorm.DB, name string) (*gorm.DB, bool) {
	var count int64
	tx := db.Model(&dbspo.MqttSender{}).Where("mqtt_user_name=?", name).Count(&count)
	return tx, count > 0
}

func (p *mqttsenderRepo) FindByProductId(db *gorm.DB, id uint64) (*gorm.DB, *dbspo.MqttSender) {
	var model dbspo.MqttSender
	return db.Model(&dbspo.MqttSender{}).Where("product_id=?", id).First(&model), &model
}
